package com.subway.babynote;

/*水*/
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

public class growthinput extends Activity implements OnClickListener {
	private static final int INVISIBLE = 0x00000004;
	private Button register_button;
	EditText weight_et;
	EditText height_et;
	Button confirm_button;
	Button login_button;
	Intent In;
	private LinearLayout linearLayout, statistics, sync, more;
	TextView hint_view;
	NotedbHelper notedb;
	float height;
	float weight;
	int user_id;
	Bundle bundle;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.growthinput);
		setTitle(R.string.growthinfo);
		In = new Intent();
	//	initTabFunction();
		notedb = new NotedbHelper(this);
		height_et = (EditText) findViewById(R.id.height_edit);
		weight_et = (EditText) findViewById(R.id.weight_edit);
		height_et.setFocusable(true);
		height_et.setInputType(InputType.TYPE_CLASS_NUMBER);
		weight_et.setInputType(InputType.TYPE_CLASS_NUMBER);
		confirm_button = (Button) findViewById(R.id.confirm_button);
		login_button = (Button) findViewById(R.id.login_button);
		hint_view = (TextView) findViewById(R.id.hint_view);
		confirm_button.setOnClickListener(this);
		login_button.setOnClickListener(this);
		hint_view.setText("");
		try {
			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
			if(notedb.getUserAge()==-1){
				In.setClass(growthinput.this, birth.class);
				startActivity(In);
			}
		} catch (Exception e) {
			Log.v("perdaystatistics", "from DB e=" + e.getMessage());
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.equals(confirm_button)) {
			try {
				user_id = syncapi.getSyncImpl().getUserid();
				if (user_id == -1) {
					user_id = notedb.getVisitorid();
				}
				height = Float.parseFloat(height_et.getText().toString());
				weight = Float.parseFloat(weight_et.getText().toString());
				if (height > 100) {
					hint_view.setText("无效值，请重输身高");
					return;
				}

				if (weight > 25) {
					hint_view.setText("无效值，请重输体重");
					return;
				}
				notedb.AddHWUser(user_id, height, weight);
		//		int ret = syncapi.getSyncImpl().informSync(
			//			syncapi.operation_type.addh.code, notedb.nowTime());
			} catch (Exception e) {
				Log.v("growthinput", "from DB e=" + e.getMessage());
			}
			bundle = new Bundle();
			bundle.putFloat("height", height);
			bundle.putFloat("weight", weight);
			In.setClass(growthinput.this, growthinfo.class);
			In.putExtras(bundle);
			startActivity(In);
		} else if (v.equals(login_button)) {

			In.setClass(growthinput.this, login.class);
			startActivity(In);
		}
	}


}